<template>
  <div>
    <el-form :inline="true" :model="user" :rules="rules" ref="ruleForm" class="demo-form-inline">
      <el-form-item label="账号" prop="name">
        <el-input v-model="user.name" placeholder="账号"></el-input>
      </el-form-item>
      <el-form-item label="密码" prop="password">
        <el-input v-model="user.password" placeholder="密码"></el-input>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="login('ruleForm')">登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  name: "LoginView",
  data() {
    return {
      user:{},
      rules: {
        username: [
          {required: true, message: '请输入', trigger: 'blur'},
        ],
        password: [
          {required: true, message: '请输入', trigger: 'blur'},
        ],
      }
    }
  }, methods: {
    login(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("http://localhost:9999/auth/login",this.user).then(res=>{
            if(res.data.code==200){
              this.$message.success(res.data.msg);
              sessionStorage.setItem("token",res.data.data)
              this.$router.push("/user")
            }else{
              this.$message.error(res.data.msg);
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
  }, created() {

  }
}
</script>

<style scoped>

</style>
